<template>
  <div class="xtx-cart-page">
    <div class="container">
      <xys-bread>
        <xys-bread-item to="/">首页</xys-bread-item>
        <xys-bread-item>购物车</xys-bread-item>
      </xys-bread>
      <div class="cart">
        <table>
          <thead>
          <tr>
            <th width="120"><xys-checkbox :modelValue="cartStore.isCheckAll" @change='checkAllSelect'>全选</xys-checkbox></th>
            <th width="400">商品信息</th>
            <th width="220">单价</th>
            <th width="180">数量</th>
            <th width="180">小计</th>
            <th width="140">操作</th>
          </tr>
          </thead>
          <!-- 有效商品 -->
          <tbody>
          <tr v-for="item in cartStore.validList" :key="item.skuId">
            <td><xys-checkbox v-model="item.selected" @change="cartStore.updateCart(item)"/></td>
            <td>
              <div class="goods">
                <RouterLink :to="`/product/${item.id}`"><img :src="item.picture" alt=""></RouterLink>
                <div>
                  <p class="name ellipsis">{{item.name}}</p>
                  <!-- 选择规格组件 -->
                  <p class="attr">{{item.attrsText}}</p>
                </div>
              </div>
            </td>
            <td class="tc">
              <p>&yen;{{item.nowPrice}}</p>
              <p v-if="item.price-item.nowPrice>0"></p>
<!--                <span class="red">&yen;{{item.price-item.nowPrice}}</span>-->
            </td>
            <td class="tc">
              <xys-numbox @change="cartStore.updateCart(item)" v-model="item.count" :max='item.stock' />
            </td>
            <td class="tc"><p class="f16 red">&yen;{{item.nowPrice*100*item.count/100}}</p></td>
            <td class="tc">
              <p><a class="green" href="javascript:;"  @click="deleteCart(item.skuId)">删除</a></p>
              <p><a href="javascript:;">找相似</a></p>
            </td>
          </tr>
          </tbody>
          <!-- 无效商品 -->
        </table>
      </div>
      <!-- 操作栏 -->
      <div class="action">
        <div class="batch">
          <xys-checkbox :model-value="cartStore.isCheckAll" @change="checkAllSelect">全选</xys-checkbox>
          <a href="javascript:;" @click="batchDeleteCart">删除商品</a>
        </div>
        <div class="total">
          共 {{cartStore.validTotal}} 件商品，已选择 {{cartStore.selectedTotal}} 件，商品合计：
          <span class="red">¥{{cartStore.selectedAmount}}</span>
          <xys-button type="primary">下单结算</xys-button>
        </div>
      </div>
<!--      &lt;!&ndash; 猜你喜欢 &ndash;&gt;-->
<!--      <goods-relevant />-->
    </div>
  </div>
</template>
<script setup>

import XysCheckbox from "@/components/library/xys-checkbox.vue";
import XysNumbox from "@/components/library/xys-numbox.vue";
import XysButton from "@/components/library/xys-button.vue";
import {useCartStore} from "@/stores/cart";
import GoodsSku from "@/views/goods/components/goods-sku.vue";
const cartStore=useCartStore()
// 单选
const checkOne = (skuId, selected) => {
  cartStore.updateCart(selected)
}
const checkAllSelect = (val) => {
  cartStore.checkAllCart(val)
}
// 删除
const deleteCart = (skuId) => {
cartStore.deleteCart(skuId)
}
// 删除选中的商品(批量删除)
const batchDeleteCart = (isInvalid) => {
cartStore.batchDeleteCart(isInvalid)
}
const getSkuIdFn = (oldSkuId, newSku) => {
cartStore.updateCartSku({ oldSkuId, newSku})
}
</script>
<style scoped lang="less">
.tc {
  text-align: center;
  .xtx-numbox {
    margin: 0 auto;
    width: 120px;
  }
}
.red {
  color: @priceColor;
}
.green {
  color: @xtxColor
}
.f16 {
  font-size: 16px;
}
.goods {
  display: flex;
  align-items: center;
  img {
    width: 100px;
    height: 100px;
  }
  > div {
    width: 280px;
    font-size: 16px;
    padding-left: 10px;
    .attr {
      font-size: 14px;
      color: #999;
    }
  }
}
.action {
  display: flex;
  background: #fff;
  margin-top: 20px;
  height: 80px;
  align-items: center;
  font-size: 16px;
  justify-content: space-between;
  padding: 0 30px;
  .xtx-checkbox {
    color: #999;
  }
  .batch {
    a {
      margin-left: 20px;
    }
  }
  .red {
    font-size: 18px;
    margin-right: 20px;
    font-weight: bold;
  }
}
.tit {
  color: #666;
  font-size: 16px;
  font-weight: normal;
  line-height: 50px;
}
.xtx-cart-page {
  .cart {
    background: #fff;
    color: #666;
    table {
      border-spacing: 0;
      border-collapse: collapse;
      line-height: 24px;
      th,td{
        padding: 10px;
        border-bottom: 1px solid #f5f5f5;
        &:first-child {
          text-align: left;
          padding-left: 30px;
          color: #999;
        }
      }
      th {
        font-size: 16px;
        font-weight: normal;
        line-height: 50px;
      }
    }
  }
}
</style>
